﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SMS.StockUsers
{
    public partial class NewStockUser : System.Web.UI.Page
    {
        SMS.Business.Stocks.StockUsersBusiness SUB;
        SMS.Business.Stocks.StocksBusiness SB;
        SMS.Business.Users.UsersBusiness UB;


        List<SMS.DAL.User> WorkersInStock
        {
            set

            { ViewState["WorkersFound"] = value; }

            get
            {
                return (List<SMS.DAL.User>)ViewState["WorkersFound"];
            }
        }
        List<SMS.DAL.User> AllWorkers
        {
            set

            { ViewState["AllWorkers"] = value; }

            get
            {
                return (List<SMS.DAL.User>)ViewState["AllWorkers"];
            }
        }
        List<SMS.DAL.User> Workerstemp
        {
            set

            { ViewState["temp"] = value; }

            get
            {
                return (List<SMS.DAL.User>)ViewState["temp"];
            }
        }
        SMS.DAL.User CurrentAdmin
        {
            set

            { ViewState["CurrentAdmin"] = value; }

            get
            {
                return (SMS.DAL.User)ViewState["CurrentAdmin"];
            }
        }
        List<SMS.DAL.User> AvailableAdmins
        {
            set

            { ViewState["AvailableAdmins"] = value; }

            get
            {
                return (List<SMS.DAL.User>)ViewState["AvailableAdmins"];
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {


            SB = new Business.Stocks.StocksBusiness();
            SUB = new Business.Stocks.StockUsersBusiness();
            UB = new Business.Users.UsersBusiness();

            if (!IsPostBack)
            {

                drpStocks.DataSource = SB.Search(new DAL.Providers.StockSearchCriteria() { Name = string.Empty, type = null });
                drpStocks.DataTextField = "Name";
                drpStocks.DataValueField = "ID";
                drpStocks.DataBind();

                AllWorkers = UB.GetAvaialableWorkers();
                CurrentAdmin = UB.Search(new DAL.Providers.UserSearchCriteria() { RoleID = 2, StockID = int.Parse(drpStocks.SelectedValue) }).SingleOrDefault();
                WorkersInStock = Workerstemp = UB.Search(new DAL.Providers.UserSearchCriteria() { RoleID = 3, StockID = int.Parse(drpStocks.SelectedValue) });
                AvailableAdmins = UB.Search(new DAL.Providers.UserSearchCriteria() { StockID = null, RoleID = 2 });
                ListsBinding();

            }
        }

        private void ListsBinding()
        {
            lstALlWorkers.DataSource = AllWorkers;
            lstALlWorkers.DataTextField = "UserName";
            lstALlWorkers.DataValueField = "ID";
            lstALlWorkers.DataBind();

            lstStockWorkers.DataSource = WorkersInStock;
            lstStockWorkers.DataTextField = "UserName";
            lstStockWorkers.DataValueField = "ID";
            lstStockWorkers.DataBind();

            drpAdmins.DataSource = AvailableAdmins;
            drpAdmins.DataTextField = "UserName";
            drpAdmins.DataValueField = "ID";
            drpAdmins.DataBind();

            if (CurrentAdmin != null)
                drpAdmins.Items.FindByValue(CurrentAdmin.ID.ToString()).Selected = true;
            else
                drpAdmins.Items.Add(new ListItem() { Selected = true, Text = "لا يوجد", Value = "-1" });
          
        }

        protected void drpStocks_SelectedIndexChanged(object sender, EventArgs e)
        {
            WorkersInStock = UB.Search(new DAL.Providers.UserSearchCriteria() { RoleID = 3, StockID = int.Parse(drpStocks.SelectedValue) });
            CurrentAdmin = UB.Search(new DAL.Providers.UserSearchCriteria() { RoleID = 2, StockID = int.Parse(drpStocks.SelectedValue) }).First();
            ListsBinding();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Workerstemp.Count; i++)
            {
                Workerstemp[i].CurrentStock = null;
                UB.Update(Workerstemp[i]);
            }
            //Hold Stock ID
            int SID = int.Parse(drpStocks.SelectedValue);

            for (int i = 0; i < WorkersInStock.Count; i++)
            {
                WorkersInStock[i].CurrentStock = SID;
                UB.Update(WorkersInStock[i]);
                if (SUB.Search(new DAL.Providers.StockUserSearchCriteria() { StockID = SID, UserID = WorkersInStock[i].ID }) == null)
                    SUB.Insert(new DAL.StockUser() { StockID = SID, UserID = WorkersInStock[i].ID });
            }

        }

        protected void btnAdminSave_Click(object sender, EventArgs e)
        {
            if (CurrentAdmin == null)
            {
                AvailableAdmins[drpAdmins.SelectedIndex - 1].CurrentStock = int.Parse(drpStocks.SelectedValue);
                UB.Update(AvailableAdmins[drpAdmins.SelectedIndex - 1]);
                SUB.Insert(new DAL.StockUser() { StockID = int.Parse(drpStocks.SelectedValue), UserID = AvailableAdmins[drpAdmins.SelectedIndex - 1].ID });
            }
            else
            {
                if (CurrentAdmin.ID != int.Parse(drpAdmins.SelectedValue))
                {
                    CurrentAdmin.CurrentStock = null;
                    UB.Update(CurrentAdmin);
                    AvailableAdmins[drpAdmins.SelectedIndex - 1].CurrentStock = int.Parse(drpStocks.SelectedValue);
                    UB.Update(AvailableAdmins[drpAdmins.SelectedIndex - 1]);
                    SUB.Insert(new DAL.StockUser() { StockID = int.Parse(drpStocks.SelectedValue), UserID = AvailableAdmins[drpAdmins.SelectedIndex - 1].ID });
                }
            }
        }
        protected void BtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (lstALlWorkers.SelectedIndex != -1)
            {
                WorkersInStock.Add(AllWorkers[lstALlWorkers.SelectedIndex]);
                AllWorkers.RemoveAt(lstALlWorkers.SelectedIndex);
                ListsBinding();
            }
        }

        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            if (lstStockWorkers.SelectedIndex != -1)
            {
                AllWorkers.Add(WorkersInStock[lstStockWorkers.SelectedIndex]);
                WorkersInStock.RemoveAt(lstStockWorkers.SelectedIndex);
                ListsBinding();
            }

        }
    }
}